<template>
  <a-modal
    :title="isShow ? '修改用户' : '新建用户'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
    okText="确定"
    cancelText="取消"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show = "false" label="主键ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item v-show="model == null" label="用户名">
          <a-input v-decorator="['username', {rules: [{required: true, message: '用户名必须填写'}]}]" />
        </a-form-item>
        <a-form-item v-if="model == null" label="密码">
          <a-input v-decorator="['password',  {rules: [{required: true, min: 5, message: '请输入至少五个字符的规则描述！'}]}]" />
        </a-form-item>
        <a-form-item label="昵称">
          <a-input v-decorator="['nickname', {rules: [{required: true, message: '请输入昵称'}]}]" />
        </a-form-item>
        <a-form-item label="电话">
          <a-input v-decorator="['mobile', {rules: [{required: true, min: 5, message: '请输入至少五个字符的规则描述！'}]}]" />
        </a-form-item>
        <a-form-item label="性别">
          <a-select v-decorator="['sex', {rules: [{required: true, message: '请选择性别'}]}]" placeholder="请选择性别">
            <a-select-option :value="1">男</a-select-option>
            <a-select-option :value="2">女</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="角色名称">
          <a-select
            v-decorator="['role_id', {rules: [{required: true, message: '请选择角色'}]}]" 
            show-search
            placeholder="请选择角色"
            option-filter-prop="children"
            :filter-option="filterOption"
          >
            <a-select-option :value="item.id" v-for="(item) in roleList" :key="item.id">
                {{item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="部门名称">
          <a-select
            v-decorator="['department_id', {rules: [{required: true, message: '请选择部门'}]}]"
            show-search
            placeholder="请选择部门"
            option-filter-prop="children"
            :filter-option="filterOption"
          >
            <a-select-option :value="item.id" v-for="(item) in department" :key="item.id">
                {{item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 引入角色列表
import { getRoLeList, getDepartmentList } from '../../../../api/system'
// 表单字段
const fields = ['id', 'username', 'password','nickname', 'sex', 'role_id', 'department_id', 'mobile']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      isShow: false,
      // 部门列表
      department: [],
      // 角色列表
      roleList: [],
      form: this.$form.createForm(this)
    }
  },
  created () {
    // 调用方法获取角色列表
    this.UsersGetRoLeList ()
    // 调用方法获取部门列表
    this.UsergetDepartmentList()

    console.log('custom modal created')

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      if(this.model && this.model.id > 0) {
        this.isShow = true
      } else {
        this.isShow = false
      }
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    // 获取角色列表
    UsersGetRoLeList () {
      getRoLeList().then(res => {
        this.roleList = res.data.data
      })
    },
    // 获取部门列表
    UsergetDepartmentList () {
      getDepartmentList().then(res => {
        const value = res.data.data.filter(item => {
          return item.status != 2
        })
        this.department = value
      })
    },
    // 搜素模糊查询
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        )
      }
  }
}
</script>
